package com.lw.lost.adapters

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.lw.lost.databinding.ItemManageFeedbackBinding
import com.lw.lost.models.Feedback

class ManageFeedbackAdapter(
    private val onFeedbackClick: (Feedback) -> Unit,
    private val onActionClick: (Feedback) -> Unit
) : ListAdapter<Feedback, ManageFeedbackAdapter.ViewHolder>(DiffCallback()) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(
            ItemManageFeedbackBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(getItem(position))
    }

    inner class ViewHolder(
        private val binding: ItemManageFeedbackBinding
    ) : RecyclerView.ViewHolder(binding.root) {

        fun bind(feedback: Feedback) {
            binding.apply {
                tvContent.text = feedback.content
                tvTime.text = feedback.feedbackTime
                tvStatus.text = if (feedback.status == 0) "待处理" else "已处理"
                
                btnAction.isEnabled = feedback.status == 0
                btnAction.text = if (feedback.status == 0) "处理" else "已处理"
                btnAction.setOnClickListener { onActionClick(feedback) }

                root.setOnClickListener { onFeedbackClick(feedback) }
            }
        }
    }

    private class DiffCallback : DiffUtil.ItemCallback<Feedback>() {
        override fun areItemsTheSame(oldItem: Feedback, newItem: Feedback) =
            oldItem.feedbackId == newItem.feedbackId

        override fun areContentsTheSame(oldItem: Feedback, newItem: Feedback) =
            oldItem == newItem
    }
} 